package com.lifeng.fastssmr.service.impl;

import java.io.IOException;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import com.lifeng.fastssmr.dao.ItemDao;
import com.lifeng.fastssmr.pojo.Item;
import com.lifeng.fastssmr.service.ItemService;
import com.lifeng.fastssmr.utils.ListAndArrayUtils;
import com.lifeng.fastssmr.vo.PageBean;
import com.mongodb.gridfs.GridFSDBFile;
@Service
public class ItemServiceImpl implements ItemService {
	@Autowired
	private ItemDao itemDao;

	@Override
	@CachePut(value="item",key="'item_id_'+ #item.id")//同时放入redis缓存,缓存中有则更新无则添加
	public void add(Item item) {
		itemDao.save(item);
	}
	
	/** 清除缓存*/
	@Override
	//@CacheEvict(value="item",key="'item_id_'+#item.id",beforeInvocation=true,allEntries=true)  
	public void delete(Item item) {
		itemDao.delete(item);
		if(!StringUtils.isEmpty(item.getImage())){
			String[] imgs = item.getImage().split("~");
			for (String string : imgs) {
				deleteFile(string);
			}
		}
	}
	
	/** 方法的返回值放入缓存*/
	@Override
	//@Cacheable(value="item",key="'item_id_'+#id")  
	public Item getById(String id) {
		return itemDao.getById(id);
	}
	
	/** 此处有问题,执行删除，添加，修改后，这个缓存不会更新，就有问题*/
	@Override
	//@Cacheable(value="item",key="'item_id_all'")  
	public List<Item> findAll() {
		return itemDao.findAll();
	}
	
	/**
	 * 保存实体,同时保存附件
	 * @throws Exception 
	 * @throws IOException 
	 */
	@Override
	public void addAndSaveFile(Item item, MultipartFile[] picFile) throws IOException, Exception {
		//先保存文件,返回文件id
		List<String> fileIds = itemDao.saveFiles(item.getId(), picFile);
		String images = ListAndArrayUtils.list2String(fileIds, "~");
		if(StringUtils.isEmpty(item.getImage())){
			item.setImage(images);
		}else{
			if(StringUtils.isEmpty(images)){
				item.setImage(item.getImage());
			}else{
				item.setImage(item.getImage()+"~"+images);
			}
		}
		itemDao.save(item);
	}
	
	/**
	 * 获取文件
	 * @throws IOException 
	 */
	@Override
	public void getImg(OutputStream out,String id) throws IOException {
		GridFSDBFile gridFSDBFile = itemDao.findOneFile(id);
		if(gridFSDBFile!=null){
			gridFSDBFile.writeTo(out);
		}
	}

	@Override
	public void deleteFile(String fileId) {
		itemDao.deleteFileByFileId(fileId);
	}

	@Override
	public PageBean getPage(int pageNum,int pageSize,Map<String,Object> param,Map<String,Object> orderPara) {
		List<Item> items = itemDao.getPageData(pageNum, pageSize, param, orderPara);
		int count = itemDao.getCount(param);
		return new PageBean(pageNum, pageSize, items, count);
	}
}
